<html>

<style>
    * {
        margin: 0;
    }

    #content {
        position: absolute;
        color: white;
        border: 1px solid rgb(71, 71, 71);
        padding: 20px;
        font-size: 14px;
    }
</style>

<script type="importmap">
	{
	  "imports": {
		"three": "../../three.js-r151/build/three.module.js",
		"three/addons/": "../../three.js-r151/examples/jsm/"
	  }
	}
</script>

<body>
    <div id="content">
        BufferGeometry通过.scale()、.translate()、.rotateX()、.rotateY()等方法可以对几何体本身进行缩放、平移、旋转. <br />
        这些方法本质上都是改变几何体的顶点数据。
    </div>
    <div id="canvas"></div>

    <script type="module">
        import * as THREE from 'three';
        import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
        import { GUI } from 'three/addons/libs/lil-gui.module.min.js';

        // 1. 创建三大件
        // 1.1 三维场景Scene, 类似创建了个世界，三维世界
        const scene = new THREE.Scene();
        const geometry = new THREE.BoxGeometry(1, 1, 1);
        const material = new THREE.MeshBasicMaterial({ color: 'yellow' });
        const cube = new THREE.Mesh(geometry, material);
        scene.add(cube);

        // 添加辅助坐标系
        const axesHelper = new THREE.AxesHelper(5);
        scene.add(axesHelper);

        // 1.2 相机
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        camera.position.set(5, 5, 5)

        // 1.3 渲染器
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        let canvasDom = document.getElementById("canvas");
        canvasDom.appendChild(renderer.domElement);

        // 设置相机控件轨道控制器OrbitControls
        const controls = new OrbitControls(camera, canvasDom);
        // 如果OrbitControls改变了相机参数，重新调用渲染器渲染三维场景
        controls.addEventListener('change', function () {
            renderer.render(scene, camera); //执行渲染操作
        });//监听鼠标、键盘事件


        // 2. 我可以改变自己
        let guiParams = {
            scale: 1,
            x: 0,
            center: false,
            rad: 0,
        }
        const gui = new GUI();
        gui.add(guiParams, 'scale', 0, 2).onChange(value => {
            //  几何体xyz三个方向都放大2倍
            geometry.scale(value, value, value)
        })

        gui.add(guiParams, 'x', 0, 5).onChange(value =>
            geometry.translate(value, 0, 0)
        );
        gui.add(guiParams, 'rad', 0, Math.PI).onChange(value =>
            geometry.rotateX(value)
        );
        gui.add(guiParams, 'center')

        function render() {
            if (guiParams.center) {
                // 居中
                geometry.center()
            }
            renderer.render(scene, camera);
            requestAnimationFrame(render)
        }
        render();
    </script>
</body>

</html>